package com.zp.drug.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.drug.entity.DrugInoutClass;
import com.zp.drug.entity.bo.DrugInoutClassBo;
import com.zp.drug.entity.vo.DrugInoutClassVo;
import com.zp.drug.mapper.DrugInoutClassMapper;
import com.zp.drug.service.IDrugInoutClassService;
import com.zp.system.api.RemoteDictService;
import com.zp.system.api.entity.SysDictData;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 出入库分类字典表Service业务层处理
 *
 * @author zhang peng
 * @ date 2023-06-20
 */
@RequiredArgsConstructor
@Service
public class DrugInoutClassServiceImpl implements IDrugInoutClassService {

    private final DrugInoutClassMapper baseMapper;

    @DubboReference
    private RemoteDictService apiDictService;

    /**
     * 查询出入库分类字典表
     */
    @Override
    public DrugInoutClassVo queryById (String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询出入库分类字典表列表(分页)
     */
    @Override
    public TableDataInfo<DrugInoutClassVo> queryPageList (DrugInoutClassBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DrugInoutClass> lqw = buildQueryWrapper(bo);
        Page<DrugInoutClassVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询出入库分类字典表列表(不分页)
     */
    @Override
    public List<DrugInoutClassVo> queryList (DrugInoutClassBo bo) {
        LambdaQueryWrapper<DrugInoutClass> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DrugInoutClass> buildQueryWrapper (DrugInoutClassBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DrugInoutClass> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getInoutType()), DrugInoutClass::getInoutType, bo.getInoutType());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), DrugInoutClass::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatisticClass()), DrugInoutClass::getStatisticClass, bo.getStatisticClass());
        lqw.eq(StringUtils.isNotBlank(bo.getStorageType()), DrugInoutClass::getStorageType, bo.getStorageType());
        lqw.eq(StringUtils.isNotBlank(bo.getFromLevel()), DrugInoutClass::getFromLevel, bo.getFromLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountFlag()), DrugInoutClass::getAccountFlag, bo.getAccountFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), DrugInoutClass::getOrgId, bo.getOrgId());
        lqw.orderByAsc(DrugInoutClass::getSort);
        return lqw;
    }

    /**
     * 新增出入库分类字典表
     */
    @Override
    public Boolean insertByBo (DrugInoutClassBo bo) {
        DrugInoutClass add = BeanUtil.toBean(bo, DrugInoutClass.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改出入库分类字典表
     */
    @Override
    public Boolean updateByBo (DrugInoutClassBo bo) {
        DrugInoutClass update = BeanUtil.toBean(bo, DrugInoutClass.class);
        update.updateInfo();
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (DrugInoutClass entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除出入库分类字典表
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据科室id 出入库类型获取库房对应的出入库方式
     *
     * @param bo
     * @return
     */
    @Override
    public List<DrugInoutClassVo> getStoreroomDistinguish (DrugInoutClassBo bo) {
        // 获取出入库方式
        List<DrugInoutClassVo> list = baseMapper.getStoreroomDistinguish(bo);
        // 获取出入库方式字典
        List<SysDictData> dictDataList = apiDictService.getDataList(bo.getDictType());
        list.forEach(item -> {
            // 通过DictValue 过滤 对应的字典对象
            List<SysDictData> retdataList = dictDataList.stream().filter(user ->
                    StringUtils.equals(user.getDictValue(), item.getType())).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(retdataList)) {
                SysDictData sysDictData = new SysDictData();
                sysDictData.setDictValue(retdataList.get(0).getDictValue());
                sysDictData.setDictLabel(retdataList.get(0).getDictLabel());
                item.setSysDictData(sysDictData);
            }
        });
        return list;
    }
}
